using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
using System.Linq;

namespace IQIGame.Onigao.GamePlay
{
    public class UIShowPanel : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIShowPanel
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnApplyAll { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVSRFriendSearch { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnApplyAll", out var __tbv0);
                this.btnApplyAll = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVSRFriendSearch", out var __tbv1);
                this.loopVSRFriendSearch = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify



        #region fields
        private LoopScrollAdapter<UI_FriendSearchMould> friendCells = null;
        private List<FriendData> searchList { get; set; } = new(8);
        #endregion fields

        #region properties

        protected UIB_UIShowPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIShowPanel();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.friendCells = this.InitLoopScroll<UI_FriendSearchMould>(this.ui.loopVSRFriendSearch, this.OnSearchFriendChanged);
        }

        protected override void OnShow()
        {


        }

        protected override void OnHide()
        {

        }


        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

            this.ui.btnApplyAll.AddClickEvent(() => OnClickApplyAll().Forget());
        }



        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
        }

        protected override void OnDispose()
        {
            this.searchList = null;
        }

        #region methonds
        private void OnSearchFriendChanged(UI_FriendSearchMould module, int arg2)
        {
            var svData = searchList[arg2];
            module.Show(svData);
        }

        private async UniTaskVoid OnClickApplyAll()
        {
            var dic = FriendModule.Instance.GetSearchListData();
            List<long> list = new List<long>();
            foreach (var item in dic)
            {
                list.Add(item.Value.id);
            }
            var res = await FriendModule.NetCore.CS_applyFriends(list);
            if (res.IsFail) return;
            FriendModule.Instance.DeleteSearchListData();

        }
        internal void RefillCells()
        {
            var dic = FriendModule.Instance.GetSearchListData().ToList();
            searchList.Clear();
            foreach (var item in dic)
            {

                searchList.Add(item.Value);
            }
            this.friendCells.RefillCellsAndSelected(searchList.Count, 0);
        }
        #endregion methonds
    }
}